var mongoose = require('mongoose')
    Schema = mongoose.Schema;
var schema = new Schema({
    name: String,
    value: String
}),
    fn = schema.methods,
    modelFn = schema.statics;
modelFn.getValue = function(name) {
    
    return model.findOne({
        name: name
    }).exec().then(function(doc) {
        return doc ? Promise.resolve(doc.value) : Promise.reject();
    })
}

modelFn.setConfig = function(name, value) {
    return model.findOne({
        name: name
    }).exec().then(function(doc) {
        doc = doc || new model({
            name: name,
            value: value
        });
        doc.value = value;
        return doc.save();
    })
}

var model = mongoose.model('config', schema);
module.exports = model;